<template>
  <!-- 添加或修改付款申请单对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-row :gutter="10">
      <finance-buy-invoice-section :invoice-id="invoiceId" ref="financeBuyInvoiceSection"></finance-buy-invoice-section>
    </el-row>

    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="审核意见" prop="assessOpinion">
              <el-input type="textarea" v-model="form.assessOpinion" placeholder="请输入审核意见"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="danger" @click="submitFormReject()">退 回</el-button>
      <el-button type="primary" @click="submitFormPass()">通 过</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
import {accAdd, addAction, getAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';
import {parseTime} from "@/utils/common";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";
import FinanceBuyInvoiceSection from "@/views/develop/finance/financeBuyInvoice/FinanceBuyInvoiceSection.vue";

export default {
  name: "FinanceBuyInvoiceAssessForm",
  dicts: ["buy_invoice_status"],
  components: {
    FinanceBuyInvoiceSection,
    FileUploadShow,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        mainId: null,
      },
      // 总条数
      total: 0,
      // 表单参数
      form: {},
      invoiceId: null,
      // 表单校验
      rules: {
        assessOpinion: [
          {required: true, message: '请输入审核意见', trigger: 'blur'}
        ]
      },
      url: {
        getById: '/financeBuyInvoice/financeBuyInvoice/',
        assessReject: '/financeBuyInvoice/financeBuyInvoice/assessReject',
        assessPass: '/financeBuyInvoice/financeBuyInvoice/assessPass',
      },
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        buyId: null,
        invoiceCode: null,
        invoicePhoto: null,
        totalMoney: null,
        paidMoney: null,
        remainMoney: null,
        assessOpinion: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加付款申请单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.invoiceId = row.id;
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "采购发票管理(" + this.form.nos + ")";
        this.loading = false;
      });
      this.buyId = row.buyId;
      this.type = row.type;
    },

    /** 提交按钮 审核退回  */
    submitFormReject(assessStatus) {
      this.form.assessStatus = assessStatus;
      this.$refs["form"].validate(valid => {
        if (valid) {
            updateAction(this.url.assessReject, this.form).then(response => {
              this.$modal.msgSuccess("审核退回成功！");
              this.open = false;
              this.$emit('ok');
            });
        }
      });
    },

    /** 提交按钮  审核通过  */
    submitFormPass(assessStatus) {
      this.form.assessStatus = assessStatus;
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateAction(this.url.assessPass, this.form).then(response => {
            this.$modal.msgSuccess("审核通过成功！");
            this.open = false;
            this.$emit('ok');
          });
        }
      });
    },

    getSummariesRel(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "amount") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },


  }
};
</script>
